<?php

namespace App\Http\Controllers\User;

use App\Library\User\CollectTrait;

class CollectController extends BaseController
{
    use CollectTrait;

    /**
     * @param string $type
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index($datatype = 'item'){

        $q = $this->request->get('q');
        $this->assign([
            'menu' => 'collect',
            'tab' => $datatype,
            'q' => $q
        ]);

        $query = $this->user()->collects()->where('datatype', $datatype);
        if ($q) $query = $query->where('title', 'LIKE', "%$q%");

        $items = $query->orderByDesc('id')->paginate(10);
        $this->assign([
            'items'=>$items,
            'pagination'=>$items->appends($q ? ['q'=>$q] : [])->render()
        ]);

        return $this->view('user.collection.'.$datatype);
    }
}
